home *** CD-ROM | disk | FTP | other *** search
/ Shareware Super Platinum 8 / Shareware Super Platinum 8.iso / mac / PROGTOOL / FLI106C.ZIP;1 / MASKNUM.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-12  |  3.6 KB  |  174 lines

  1. //
  2. // The Fusion Library Interface for DOS
  3. // Version 1.06c
  4. // Copyright (C) 1990, 1991, 1992
  5. // Software Dimensions
  6. //
  7. // NumberMask
  8. //
  9.  
  10. #include "fli.h"
  11.  
  12. #ifdef __BCPLUSPLUS__
  13. #pragma hdrstop
  14. #endif
  15.  
  16. #include <ctype.h>
  17. #include <string.h>
  18. #include <alloc.h>
  19.  
  20. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  21. //
  22. // CountPlaces()
  23. //
  24. // Counts # of digits before and after the decimal place
  25. //
  26. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  27.  
  28. void NumberMask::CountPlaces(char *Mask,int &Before,int &After)
  29. {
  30.   char *DiscardableMask=strchr(Mask,'.');
  31.   char *SecondDiscardableMask=DiscardableMask;
  32.  
  33.   After=0;
  34.   Before=0;
  35.  
  36.   if (DiscardableMask)
  37.   {
  38.     DiscardableMask++;
  39.     while (*DiscardableMask)
  40.     {
  41.       if (*DiscardableMask=='#')
  42.         After++;
  43.       DiscardableMask++;
  44.     }
  45.   }
  46.  
  47.   if (!SecondDiscardableMask)
  48.     SecondDiscardableMask=Mask+strlen(Mask)-1;
  49.  
  50.   do
  51.   {
  52.     if (*SecondDiscardableMask=='#')
  53.       Before++;
  54.   }
  55.   while ((SecondDiscardableMask--)!=Mask);
  56. }
  57.  
  58. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  59. //
  60. // MaskShow()
  61. //
  62. // Handles displaying numbers within a mask
  63. //
  64. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  65.  
  66. void NumberMask::MaskShow(char *_Mask,char *Value,BlazeClass &Blaze)
  67. {
  68.   char *Position;
  69.  
  70.   if (Position=strchr(Value,'.'),Position) // Failsafe - strip leading zeros
  71.   {
  72.     int ZeroCount=0;
  73.     int OtherCount=0;
  74.     Position--;
  75.     do
  76.     {
  77.       if (isdigit(*Position))
  78.       {
  79.         if (*Position!='0')
  80.         {
  81.           OtherCount++;
  82.           break;
  83.         }
  84.         else
  85.           ZeroCount++;
  86.       }
  87.       Position--;
  88.     } while (Position>=Value);
  89.  
  90.     if (!OtherCount && ZeroCount)
  91.     {
  92.       Position=Value;
  93.       if (*Position=='-')
  94.         Position++;
  95.       do
  96.       {
  97.         memmove(Position,Position+1,strlen(Position)+1);
  98.       } while (*Position!='.');
  99.     }
  100.   }
  101.  
  102.   char *Mask=strdup(_Mask);
  103.  
  104.   if (!*Value)
  105.   {
  106.     *Value='0';
  107.     *(Value+1)=0;
  108.   }
  109.  
  110.   char *ValueDecimals=strchr(Value,'.');
  111.   char *MaskDecimals=strchr(Mask,'.');
  112.  
  113.   char *SecondValueDecimals=ValueDecimals;
  114.   char *SecondMaskDecimals=MaskDecimals;
  115.  
  116.   if (ValueDecimals)
  117.   {
  118.     ValueDecimals++;
  119.     SecondValueDecimals--;
  120.   }
  121.  
  122.   if (MaskDecimals)
  123.   {
  124.     while (*MaskDecimals)
  125.     {
  126.       if (*MaskDecimals=='#' && (!ValueDecimals || !*ValueDecimals))
  127.         *MaskDecimals='0';
  128.       else if (*MaskDecimals=='#' && ValueDecimals)
  129.         *MaskDecimals=*ValueDecimals++;
  130.       MaskDecimals++;
  131.     }
  132.   }
  133.  
  134.   if (!SecondMaskDecimals)
  135.     SecondMaskDecimals=Mask+strlen(Mask)-1;
  136.  
  137.   if (!SecondValueDecimals)
  138.     SecondValueDecimals=Value+strlen(Value)-1;
  139.  
  140.   do
  141.   {
  142.     if ((*SecondMaskDecimals=='#' || *SecondMaskDecimals==',') &&
  143.         (SecondValueDecimals==Value-1 || *SecondValueDecimals=='-'))
  144.       *SecondMaskDecimals=' ';
  145.     else if (*SecondMaskDecimals=='#' && SecondValueDecimals!=Value-1)
  146.       *SecondMaskDecimals=*SecondValueDecimals--;
  147.   }
  148.   while ((SecondMaskDecimals--)!=Mask);
  149.  
  150.   char *Locator;
  151.  
  152.   if ((Locator=strchr(Mask,'-'),Locator) && !strchr(Value,'-'))
  153.     *Locator=' ';
  154.  
  155.   if ((Locator=strchr(Mask,'+'),Locator) && strchr(Value,'-'))
  156.     *Locator='-';
  157.  
  158.   if ((Locator=strchr(Mask,'('),Locator) && !strchr(Value,'-'))
  159.   {
  160.     *Locator=' ';
  161.     if (Locator=strchr(Mask,')'),Locator)
  162.       *Locator=' ';
  163.   }
  164.  
  165.   if (SecondValueDecimals>Value-1 && *SecondValueDecimals!='-')
  166.     strnset(Mask,'*',strlen(Mask)); // OVERFLOW
  167.  
  168.   Blaze << Mask;
  169.  
  170.   free(Mask);
  171. }
  172.  
  173.  
  174.